<template>
  <el-dialog
    :title="!form.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    width="500px"
    :visible.sync="visible">
    <el-form
      size="mini"
      :model="form"
      :rules="rules"
      ref="dataForm"
      @keyup.enter.native="submitForm()"
      label-width="80px">
      <el-form-item label="仓库名称" prop="name">
        <el-input v-model="form.name" placeholder="仓库名称"></el-input>
      </el-form-item>
      <el-form-item label="仓库地址" prop="address">
        <el-input v-model="form.address" placeholder="仓库地址"></el-input>
      </el-form-item>
      <el-form-item label="区域编码" prop="areaCode">
        <el-input v-model="form.areaCode" placeholder="区域编码"></el-input>
      </el-form-item>
      <el-form-item label="区域编码" prop="areaCode">
        <el-input v-model="form.areaCode" placeholder="区域编码"></el-input>
      </el-form-item>
      <el-row>
        <el-col :span="12">
          <el-form-item label="排序" prop="sort">
            <el-input-number v-model="form.sort" placeholder="仓库地址"></el-input-number>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="状态" prop="status">
            <el-switch
              v-model="form.status"
              :active-value="1"
              :inactive-value="0"
            ></el-switch>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="mini" icon="el-icon-circle-check" type="primary" @click="submitForm()">确定</el-button>
      <el-button size="mini" icon="el-icon-circle-close" @click="visible = false">取消</el-button>
    </span>
  </el-dialog>
</template>

<script>
import {getWare, saveWare, updateWare} from "@/api/ware/ware";

export default {
  name: 'WareForm',
  data() {
    return {
      visible: false,
      form: {
        status: 1,
        sort: 1
      },
      rules: {
        name: [
          {required: true, message: '请输入仓库名称', trigger: 'blur'}
        ],
        address: [
          {required: true, message: '请输入仓库地址', trigger: 'blur'}
        ],
        areaCode: [
          {required: true, message: '请输入区域编码', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    add() {
      this.visible = true
      this.$refs['dataForm'].resetFields()
      this.form = {
        status: 1,
        sort: 1
      }
    },

    edit(id) {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        getWare(id).then(res => {
          if (res.success) {
            this.form = res.data
          }
        })
      })
    },

    submitForm() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          if (!this.form.id) {
            saveWare(this.form).then(res => {
              if (res.success) {
                this.visible = false
                this.$message.success('新增成功')
                this.$emit('ok')
              }
            })
          } else {
            updateWare(this.form).then(res => {
              if (res.success) {
                this.visible = false
                this.$message.success('更新成功')
                this.$emit('ok')
              }
            })
          }
        }
      })
    }
  }
}
</script>
